// Copyright 2023 The titan Authors. All rights reserved.

// Project: titan
// IDE: GoLand
// Author: fucaihe@outlook.com
// File: arith.go
// Date: 2023/12/18 11:18:54
// Description: RPC接口 - 处理算术相关 - DEMO

package api

import (
	"context"
	"time"

	"go.uber.org/zap"
)

// Arith 接口对象
type Arith struct {
	*API
}

// ArithArgs 参数
type ArithArgs struct {
	A float64
	B float64
}

// ArithReply 结果
type ArithReply struct {
	C float64
}

// ArithAPI 接口
type ArithAPI interface {
	// Add 加法运算
	Add(ctx context.Context, args *ArithArgs, reply *ArithReply) error

	// Sub 减法运算
	Sub(ctx context.Context, args *ArithArgs, reply *ArithReply) error

	// Mul 乘法运算
	Mul(ctx context.Context, args *ArithArgs, reply *ArithReply) error

	// Div 除法运算
	Div(ctx context.Context, args *ArithArgs, reply *ArithReply) error
}

// Add 实现加法运算
func (a *Arith) Add(ctx context.Context, args *ArithArgs, reply *ArithReply) error {
	now := time.Now()
	defer func() {
		a.logger.Info("Arith Add", zap.Int64("cost", time.Since(now).Milliseconds()))
	}()

	reply.C = args.A + args.B
	return nil
}

// Sub 实现减法运算
func (a *Arith) Sub(ctx context.Context, args *ArithArgs, reply *ArithReply) error {
	now := time.Now()
	defer func() {
		a.logger.Info("Arith Sub", zap.Int64("cost", time.Since(now).Milliseconds()))
	}()

	reply.C = args.A - args.B
	return nil
}

// Mul 实现乘法运算
func (a *Arith) Mul(ctx context.Context, args *ArithArgs, reply *ArithReply) error {
	now := time.Now()
	defer func() {
		a.logger.Info("Arith Mul", zap.Int64("cost", time.Since(now).Milliseconds()))
	}()

	reply.C = args.A * args.B
	return nil
}

// Div 实现除法运算
func (a *Arith) Div(ctx context.Context, args *ArithArgs, reply *ArithReply) error {
	now := time.Now()
	defer func() {
		a.logger.Info("Arith Div", zap.Int64("cost", time.Since(now).Milliseconds()))
	}()

	reply.C = args.A / args.B
	return nil
}

// NewArithAPI 新建接口对象
func NewArithAPI(a *API) ArithAPI {
	return &Arith{API: a}
}
